package com.qf.cx.simplenms.link.rai;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qf.cx.simplenms.link.rai.model.Antenna.ByReference;
import com.qf.cx.simplenms.link.rai.model.InventoryResult;
import com.qf.cx.simplenms.link.rai.model.RwPara;
import com.qf.cx.simplenms.link.rai.model.RwResult;
import com.sun.jna.ptr.IntByReference;

public class RaiLibraryWrapper implements RaiLibrary {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	private RaiLibrary raiLibrary;

	public RaiLibraryWrapper(RaiLibrary raiLibrary) {
		this.raiLibrary = raiLibrary;
	}

	@Override
	public int rai_inventory(int fd, ByReference antenna, InventoryResult.ByReference result) {
		try {
			logger.debug("calling rai_inventory, fd = {}, antenna = {}", fd, antenna);
			int ret = raiLibrary.rai_inventory(fd, antenna, result);
			logger.debug("called rai_inventory, fd = {}, antenna = {}", fd, antenna);
			return ret;
		} catch (java.lang.Error e) {
			logger.error("error on callling rai_inventory, fd = {}, antenna = {}", fd, antenna, e);
			throw new RaiException(e);
		}
	}

	@Override
	public int rai_startPeriodOp(int fd, RwPara.ByReference antenna, int timespan) {
		try {
			logger.debug("calling rai_startPeriodOp, fd = {}, antenna = {}, timespan = {}", fd, antenna, timespan);
			int ret = raiLibrary.rai_startPeriodOp(fd, antenna, timespan);
			logger.debug("called rai_startPeriodOp, fd = {}, antenna = {}, timespan = {}", fd, antenna, timespan);
			return ret;
		} catch (java.lang.Error e) {
			logger.error("error on callling rai_startPeriodOp, fd = {}, antenna = {}, timespan = {}", fd, antenna,
					timespan, e);
			throw new RaiException(e);
		}
	}

	@Override
	public int rai_getPeriodOpResult(int fd, int timeout, RwResult.ByReference result) {
		try {
			logger.debug("calling rai_getPeriodOpResult, fd = {}, timeout = {}", fd, timeout);
			int ret = raiLibrary.rai_getPeriodOpResult(fd, timeout, result);
			logger.debug("called rai_getPeriodOpResult, fd = {}, timeout = {}", fd, timeout);
			return ret;
		} catch (java.lang.Error e) {
			logger.error("error on callling rai_getPeriodOpResult, fd = {}, timeout = {}", fd, timeout, e);
			throw new RaiException(e);
		}
	}

	@Override
	public int rai_stopPeriodOp(int fd) {
		try {
			logger.debug("calling rai_stopPeriodOp, fd = {}", fd);
			int ret = raiLibrary.rai_stopPeriodOp(fd);
			logger.debug("called rai_stopPeriodOp, fd = {}", fd);
			return ret;
		} catch (java.lang.Error e) {
			logger.error("error on callling rai_stopPeriodOp, fd = {}", fd, e);
			throw new RaiException(e);
		}
	}

	@Override
	public int rai_connect(String url, IntByReference fd) {
		try {
			logger.debug("calling rai_connect, url = {}", url);
			int ret = raiLibrary.rai_connect(url, fd);
			logger.debug("called rai_connect, url = {}", url);
			return ret;
		} catch (java.lang.Error e) {
			logger.error("error on callling rai_connect, url = {}", url, e);
			throw new RaiException(e);
		}
	}

	@Override
	public int rai_disConnect(int fd) {
		try {
			logger.debug("calling rai_disConnect, fd = {}", fd);
			int ret = raiLibrary.rai_disConnect(fd);
			logger.debug("called rai_disConnect, fd = {}", fd);
			return ret;
		} catch (java.lang.Error e) {
			logger.error("error on callling rai_disConnect, fd = {}", fd, e);
			throw new RaiException(e);
		}
	}

	@Override
	public int rai_getLinkStatus(int fd) {
		try {
			logger.debug("calling rai_getLinkStatus, fd = {}", fd);
			int ret = raiLibrary.rai_getLinkStatus(fd);
			logger.debug("called rai_getLinkStatus, fd = {}", fd);
			return ret;
		} catch (java.lang.Error e) {
			logger.error("error on callling rai_getLinkStatus, fd = {}", fd, e);
			throw new RaiException(e);
		}
	}

	@Override
	public int rai_startListen(String url, IntByReference fd) {
		try {
			logger.debug("calling rai_startListen, url = {}", url);
			int ret = raiLibrary.rai_startListen(url, fd);
			logger.debug("called rai_startListen, url = {}", url);
			return ret;
		} catch (java.lang.Error e) {
			logger.error("error on callling rai_startListen, url = {}", url, e);
			throw new RaiException(e);
		}
	}

	@Override
	public int rai_getListenLink(int listenfd, IntByReference fd) {
		try {
			logger.debug("calling rai_getListenLink, listenfd = {}", listenfd);
			int ret = raiLibrary.rai_getListenLink(listenfd, fd);
			logger.debug("called rai_getListenLink, listenfd = {}", listenfd);
			return ret;
		} catch (java.lang.Error e) {
			logger.error("error on callling rai_getListenLink, listenfd = {}", listenfd, e);
			throw new RaiException(e);
		}
	}

	@Override
	public int rai_stopListen(int listenfd) {
		try {
			logger.debug("calling rai_stopListen, listenfd = {}", listenfd);
			int ret = raiLibrary.rai_stopListen(listenfd);
			logger.debug("called rai_stopListen, listenfd = {}", listenfd);
			return ret;
		} catch (java.lang.Error e) {
			logger.error("error on callling rai_stopListen, listenfd = {}", listenfd, e);
			throw new RaiException(e);
		}
	}

	@Override
	public int rai_getDeviceSN(int fd, byte[] deviceSN) {
		try {
			logger.debug("calling rai_getDeviceSN, fd = {}", fd);
			int ret = raiLibrary.rai_getDeviceSN(fd, deviceSN);
			logger.debug("called rai_getDeviceSN, fd = {}", fd);
			return ret;
		} catch (java.lang.Error e) {
			logger.error("error on callling rai_getDeviceSN, fd = {}", fd, e);
			throw new RaiException(e);
		}
	}

	public static class RaiException extends RuntimeException {
		private static final long serialVersionUID = 1L;

		public RaiException(Error e) {
			super(e);
		}
	}
}
